<?php

class Form_Playlist extends Zend_Form
{
    const MAX_ARTISTS = 10;

    public $buttonDecorators = array(
        'ViewHelper',
        array(array('data' => 'HtmlTag'), array('tag' => 'dd', 'class' => 'block')));

    public function init()
    {
        $this->setMethod(self::METHOD_POST);


        $fmUser = LastFM_User::getActiveUser();

        $playlistName = $this->createElement('text', 'playlist_name');
        $playlistName->addFilter('StringTrim');
        $playlistName->setLabel('The name of playlist');
        $playlistName->setValue("New Playlist");
        $this->addElement($playlistName);

        $friends = $fmUser->getFriends();
        $options = array($fmUser->getName() => $fmUser->getName());
        $pictures = array($fmUser->getName() => (string) $fmUser->getInfo()->image[0]);

        foreach ($friends->user as $friend)
        {
            $name = (string) $friend->name;
            $realname = (string) $friend->realname;

            $friend_name = $name;

            if (!empty($realname))
            {
                $friend_name .= ' (' . $realname . ')';
            }

            $options[$name] = $friend_name;
            $pictures[$name] = (string) $friend->image[0];
        }



        $friends = new My_Form_Element_MultiCheckboxPictures('friends');
        //$friends->setLabel('Friends');
        $friends->setValue($fmUser->getName());
        $friends->setMultiOptions($options);
        $friends->setPictures($pictures);

        $this->addElement($friends);

        $this->addDisplayGroup(array('friends'), 'group_friends', array('legend' => 'Friends'));


        $my_artists = $fmUser->getTopArtists();
        $options = array();
        $pictures = array();


        for ($i = 0; $i < self::MAX_ARTISTS; $i++)
        {
            $artist_name = (string) $my_artists->artist[$i]->name;
            $pictures[$artist_name] = (string) $my_artists->artist[$i]->image[0];

            $options[$artist_name] = $artist_name;
        }

        $artists = new My_Form_Element_MultiCheckboxPictures('artists');
        //$artists->setLabel('Include following artists');
        $artists->setMultiOptions($options);
        $artists->setPictures($pictures);
        $this->addElement($artists);

        $this->addDisplayGroup(array('artists'), 'group_artists', array('legend' => 'Artists'));

        $add_songs = $this->createElement('textarea', 'add_songs');
        $add_songs->setLabel('Additional Songs');
        $add_songs->setAttrib('cols', 40);
        $add_songs->setAttrib('rows', 5);
        $add_songs->setDescription("Example: U2 - Beautiful Day");
        $this->addElement($add_songs);


        $my_limit = array(10, 20, 50, 80);

        $limit = $this->createElement('select', 'limit');
        $limit->setLabel('Max. limit');
        $limit->setMultiOptions(array_combine($my_limit, $my_limit));
        $limit->setValue(20);
        $limit->addFilter('Int');
        $this->addElement($limit);

        $outputType = new Zend_Form_Element_Radio('outputType');
        $outputType->setLabel('Choose type of format:');
        $outputType->addMultiOptions(array('html' => 'show','tinysong' => 'show + play songs','grooveshark' => 'play the whole playlist'));
        //$outputType->addMultiOptions(array('html' => 'show','tinysong' => 'show + play songs','xml' => 'generate the playlist in format XSPF','grooveshark' => 'play the whole playlist'));
        $outputType->setAttrib('checked', 'checked');
        $this->addElement($outputType);

        $button = $this->createElement('button', 'Playlist_button', array("label" => "Create", "type" => "submit", "class" => "thoughtbot", "decorators" => $this->buttonDecorators));
        $this->addElement($button);

        /*
          $this->setElementDecorators(array(
          'ViewHelper',
          array(array('data' => 'HtmlTag'),  array('tag' =>'td', 'class'=> 'element')),
          array('Label', array('tag' => 'td')),
          array(array('row' => 'HtmlTag'), array('tag' => 'tr'))
          ));

          $this->setDecorators(array(
          'FormElements',
          array('HtmlTag', array('tag' => 'table')),
          'Form'
          ));
         *
         */
    }

}

